package com.group9.handinhand.dao.model;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.ArrayUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.group9.handinhand.service.so.UserEnums.UserPrivacy;
import com.group9.handinhand.service.so.UserEnums.UserStatus;
import com.group9.handinhand.service.so.UserSO;

@Entity
@Table(name = "USER")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User extends AbstractBaseModel {
	
	private static final long serialVersionUID = 1L;
	
	@Column(name = "NAME", nullable = false, length = 50)
	private String name;
	
	@Column(name = "SURNAME", nullable = false, length = 50)
	private String surname;
	
	@Column(name = "USERNAME", nullable = false,unique=true, length = 30)
	private String username;
	
	@Column(name = "EMAIL", nullable = false, unique = true, length = 75)
	private String email;
	
	@Column(name = "PASSWORD", length = 16)
	private String password;
	
	@Enumerated(EnumType.STRING)
	@Column(name = "STATUS")
	private UserStatus status;
	
	@Enumerated(EnumType.STRING)
	@Column(name = "PRIVACY")
	private UserPrivacy userPrivacy;

	@Column(name = "MOBILE", length = 12)
	private String mobile;
	
	@Column(name = "PHONE", length = 12)
	private String phone;

	@Column(name = "ADDRESS", length = 150)
	private String address;
	
	@Column(name = "CITY", length = 30)
	private String city;
	
	@Column(name = "COUNTRY", length = 30)
	private String country;
	
	@Column(name = "INFO", length = 500)
	private String info;
	
	@Lob
	@Column(name = "PICTURE", length=100000)
	private byte [] picture;

	@OneToMany(mappedBy="user",fetch=FetchType.EAGER)
	private Set<SocialUser> socialUsers = new HashSet<SocialUser>();
		
	public UserSO convertToServiceObject(String... ignoreProperties) {
		UserSO so = (UserSO) convert(new UserSO(),
			(String[]) ArrayUtils.addAll(ignoreProperties, new String[]{"socialUsers"}));
		if( ! Arrays.asList(ignoreProperties).contains("socialUsers"))
		for (SocialUser socialUser : socialUsers) {
			so.getSocialUsers().add(socialUser.convertToServiceObject());
		}
		return so;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSurname() {
		return surname;
	}
	public void setSurname(String surname) {
		this.surname = surname;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public UserStatus getStatus() {
		return status;
	}

	public void setStatus(UserStatus status) {
		this.status = status;
	}

	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	
	public byte[] getPicture() {
		return picture;
	}

	public void setPicture(byte[] picture) {
		this.picture = picture;
	}
	
	public UserPrivacy getUserPrivacy() {
		return userPrivacy;
	}

	public void setUserPrivacy(UserPrivacy userPrivacy) {
		this.userPrivacy = userPrivacy;
	}

	public Set<SocialUser> getSocialUsers() {
		return socialUsers;
	}

	public void setSocialUsers(Set<SocialUser> socialUsers) {
		this.socialUsers = socialUsers;
	}

	@Override
	public String toString() {
		return "User [name=" + name + ", surname=" + surname + ", username="
				+ username + ", email=" + email + ", password=" + password
				+ ", status=" + status + ", userPrivacy=" + userPrivacy
				+ ", mobile=" + mobile + ", phone=" + phone + ", address="
				+ address + ", city=" + city + ", country=" + country
				+ ", info=" + info + ", picture=" + picture + ", socialUsers="
				+ socialUsers + "]";
	}

}
